package com.nulldev.util.concurrency.threadIt.v4.emapi.testing.bench;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.concurrency.threadIt.v4.emapi.ExecutorManager;
import com.nulldev.util.concurrency.threadIt.v4.emapi.ft.FutureTask;
import com.nulldev.util.concurrency.threadIt.v4.emapi.ft2.FutureTask2;
import com.nulldev.util.io.IOUtils;
import com.nulldev.util.manage.legacy.Timer;

public class EMAPIThroughputTest {

	public static void main(String[] args) {
		final BenchmarkExecutor bm = new BenchmarkExecutor();
		final ExecutorManager em = new ExecutorManager(bm);
		final long timeLimit = 10000;
		{
			final Timer t = new Timer(1000);
			t.start();
			final Runnable r = () -> {
			};
			while (!t.hasReached(timeLimit)) {
				em.asyncSubmit(r);
			}
			t.stop();
			JVM.println("Recieved " + bm.get() + " asynchronous runnables in 10.00 seconds.");
			JVM.println("\tEstimated " + (bm.get().get() / 10) + " per second.");
		}
		try {
			JVM.println("Waiting 10 seconds for CPU caches to demote themselves...");
			Thread.sleep(10000L);
		} catch (InterruptedException e) {
		}
		{
			final Timer t = new Timer(1000);
			t.start();
			final Runnable r = () -> {
			};
			while (!t.hasReached(timeLimit)) {
				em.execute(r);
			}
			t.stop();
			JVM.println("Recieved " + bm.get() + " synchronous runnables in 10.00 seconds.");
			JVM.println("\tEstimated " + (bm.get().get() / 10) + " per second.");
		}
		{
			final Timer t = new Timer(1000);
			t.start();
			final FutureTask<Object> r = new FutureTask<Object>() {

				@Override
				public Object execute() {
					return 1;
				}
			};
			while (!t.hasReached(timeLimit)) {
				em.execute(r);
			}
			t.stop();
			JVM.println("Recieved " + bm.get() + " synchronous FT1s in 10.00 seconds.");
			JVM.println("\tEstimated " + (bm.get().get() / 10) + " per second.");
		}
		{
			final Timer t = new Timer(1000);
			t.start();
			final FutureTask2<Object> r = new FutureTask2<Object>() {

				@Override
				public Object execute() {
					return 1;
				}
			};
			while (!t.hasReached(timeLimit)) {
				em.execute(r);
			}
			t.stop();
			JVM.println("Recieved " + bm.get() + " synchronous FT2s in 10.00 seconds.");
			JVM.println("\tEstimated " + (bm.get().get() / 10) + " per second.");
		}
		IOUtils.closeQuietly(em);
	}

}
